package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.TenderSignature;

public class TenderSignatureService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;
	
	public TenderSignatureService(){}
	
	public TenderSignatureService(Context context){
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}
	
	public void DeleteAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.TenderSignature.SCHEMA_NAME);
	}
	
	public void DeleteByID(long id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM "+ DatabaseSchema.TenderSignature.SCHEMA_NAME
				+" WHERE "+ DatabaseSchema.TenderSignature.TS_ID +" = ?",
				new Object[] { id });
	}
	
	public void Update(TenderSignature obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Update " + DatabaseSchema.TenderSignature.SCHEMA_NAME + " set ts_tender_id = ?,ts_signature_value = ?,ts_trn_no = ? where ts_id = ?",
				new Object[] { obj.getTs_tender_id(), obj.getTs_signature_value(),obj.getTs_trn_no(), obj.getTs_id() });
	}
	
	
	public List<TenderSignature> FindAll(){
		List<TenderSignature> obj = new ArrayList<TenderSignature>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.TenderSignature.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ts_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_ID));
			int ts_tender_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_TENDER_ID));
			String ts_signature_value = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_SIGNATURE_VALUE));
			int ts_trn_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_TRN_NO));
			
			obj.add(new TenderSignature(ts_id, ts_tender_id, ts_signature_value,ts_trn_no));
		}
		cursor.close();
		return obj;
	}
	
	public List<TenderSignature> FindByTranNo(int tno){
		List<TenderSignature> obj = new ArrayList<TenderSignature>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.TenderSignature.SCHEMA_NAME+" where ts_trn_no = "+ tno, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ts_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_ID));
			int ts_tender_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_TENDER_ID));
			String ts_signature_value = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_SIGNATURE_VALUE));
			int ts_trn_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_TRN_NO));
			
			obj.add(new TenderSignature(ts_id, ts_tender_id, ts_signature_value,ts_trn_no));
		}
		cursor.close();
		return obj;
	}
	
	public TenderSignature Find(int tender_id,int trn_no){
		TenderSignature obj = new TenderSignature();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		
		Cursor cursor = db.rawQuery("select * from " + DatabaseSchema.TenderSignature.SCHEMA_NAME+" where ts_trn_no = "+trn_no, null);
		if (cursor == null) {
			ShowMessage(DBError.error,mMessageDialog.MESSAGE_BOX_OK,mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		if (cursor.moveToNext()) {
			int ts_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_ID));
			int ts_tender_id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_TENDER_ID));
			String ts_signature_value = cursor.getString(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_SIGNATURE_VALUE));
			int ts_trn_no = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.TenderSignature.TS_TRN_NO));
			
			obj = new TenderSignature(ts_id, ts_tender_id, ts_signature_value,ts_trn_no);
		}
		cursor.close();
		return obj;
	}
	
	public void Insert(TenderSignature obj){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO " + DatabaseSchema.TenderSignature.SCHEMA_NAME + " VALUES(?,?,?,?)",
				new Object[] { null, obj.getTs_tender_id(), obj.getTs_signature_value(),obj.getTs_trn_no()});
	}
	
	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}
	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
